package com.hongtech.modules.common;

import com.hongtech.common.bean.AjaxResultVo;
import com.hongtech.framework.config.OssConfig;
import com.hongtech.modules.oss.OssFactory;
import com.hongtech.modules.oss.SysUploadFile;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * 文件上传控制器
 */
@Controller
@RequestMapping("/oss/file")
public class FileController extends BaseController {

    public static final Logger logger = Logger.getLogger(FileController.class);

    @Autowired
    private OssConfig ossConfig;

    /**
     * 通用上传请求 (单个文件)
     */
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResultVo uploadImageFile(MultipartFile file) throws Exception {
        try {
            if (file.isEmpty()) {
                return AjaxResultVo.error("请选择文件！");
            } else {
                SysUploadFile fileResult = Objects.requireNonNull(OssFactory.init(ossConfig)).uploadFile(file, true);
                AjaxResultVo rest = AjaxResultVo.success();
                rest.put("fileName", fileResult.getOriginalName());
                rest.put("fileUrl", fileResult.getFileUrl());
                return rest;
            }
        } catch (Exception e) {
            logger.error("文件上传失败", e);
            e.printStackTrace();
            return AjaxResultVo.error("服务器异常请联系管理员！");
        }
    }

    /**
     * 通用上传请求（多个文件）
     */
    @PostMapping("/uploads")
    @ResponseBody
    public AjaxResultVo uploadFiles(MultipartFile[] file, HttpServletResponse httpResponse) throws Exception {
        try {
            if (file != null && file.length > 0) {
                List<SysUploadFile> fileInfos = new LinkedList<>();
                for (MultipartFile tempFile : file) {
                    SysUploadFile fileResult = Objects.requireNonNull(OssFactory.init(ossConfig)).uploadFile(tempFile, true);
                    fileInfos.add(fileResult);
                }
                return AjaxResultVo.success(fileInfos);
            } else {
                httpResponse.setStatus(405);
                return AjaxResultVo.error("请选择文件！");
            }
        } catch (Exception e) {
            httpResponse.setStatus(405);
            e.printStackTrace();
            return AjaxResultVo.error("服务器异常请联系管理员！");
        }
    }

    /**
     * 删除图片
     *
     * @param filePath 文件路径
     */
    @PostMapping("/deleteFiles")
    @ResponseBody
    public AjaxResultVo deleteFiles(String filePath) {
        if (StringUtils.isNotBlank(filePath)) {
            Objects.requireNonNull(OssFactory.init(ossConfig)).delete(filePath);
            return AjaxResultVo.success("删除成功！");
        }
        return AjaxResultVo.error("删除失败");
    }

}
